#include "include/shortcutpreferenceswidget.h"
#include "ui_shortcutpreferenceswidget.h"

#include <QAction>
#include <QKeySequence>
#include <QShortcut>
#include "include/shortcutdelegate.h"

ShortcutPreferencesWidget::ShortcutPreferencesWidget(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::ShortcutPreferencesWidget)
{
	ui->setupUi(this);

	m_model = new ShortcutsModel(this);
	ui->tvShortcuts->setModel(m_model);
	ui->tvShortcuts->horizontalHeader()->setResizeMode(ShortcutsModel::NameColumn, QHeaderView::Interactive);
	//ui->tvShortcuts->horizontalHeader()->setResizeMode(ShortcutsModel::NameColumn, QHeaderView::ResizeToContents);
	ui->tvShortcuts->horizontalHeader()->setResizeMode(ShortcutsModel::ShortcutColumn, QHeaderView::Stretch);

	//ui->tvShortcuts->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);

	ShortcutDelegate* delegate = new ShortcutDelegate(this);
	m_shortcutDelegate = delegate;
	ui->tvShortcuts->setItemDelegateForColumn(ShortcutsModel::ShortcutColumn, delegate);

	connect(delegate, SIGNAL(editorDisplayed(int)), this, SLOT(onEditorDisplayed(int)));
	connect(delegate, SIGNAL(editorClosed(int)), this, SLOT(onEditorClosed(int)));
}

ShortcutPreferencesWidget::~ShortcutPreferencesWidget()
{
	delete ui;
}

void ShortcutPreferencesWidget::setShortcuts(MuCustomShortcuts* shortcuts)
{
	m_model->setShortcuts(shortcuts);

	ui->tvShortcuts->resizeColumnToContents(ShortcutsModel::NameColumn);
	ui->tvShortcuts->resizeRowsToContents();
	//ui->tvShortcuts->resizeColumnsToContents();
}

void ShortcutPreferencesWidget::onEditorDisplayed(int index)
{
	ui->tvShortcuts->verticalHeader()->setResizeMode(index, QHeaderView::ResizeToContents);
}

void ShortcutPreferencesWidget::onEditorClosed(int index)
{
	ui->tvShortcuts->verticalHeader()->setResizeMode(index, QHeaderView::ResizeToContents);
}

void ShortcutPreferencesWidget::applyChanges()
{
	m_shortcutDelegate->writeEditorToModel();
	m_model->applyChanges();
}
